package com.bjyhl.medical.main.news.list;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjyhl.ui.recycler.DataConverter;
import com.bjyhl.ui.recycler.ItemType;
import com.bjyhl.ui.recycler.MultipleFields;
import com.bjyhl.ui.recycler.MultipleItemEntity;
import com.blankj.utilcode.util.StringUtils;

import java.util.ArrayList;

/**
 * 新闻数据
 */
public class NewsDataConverter extends DataConverter {

    @Override
    public ArrayList<MultipleItemEntity> convert() {
        final JSONArray dataArray = JSON.parseObject(getJsonData()).getJSONArray("data");


        final int size = dataArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = dataArray.getJSONObject(i);

            final Integer id = data.getInteger("id");
            final String title = data.getString("title");
            final String link = data.getString("link");
            final String pubDate = data.getString("pub_date");
            final String author = data.getString("author");
            final String image = data.getString("image");
            final String summary = data.getString("summary");

            int type;
            if(StringUtils.isEmpty(image)) {
                type = ItemType.NEWS;
            } else {
                type = ItemType.NEWS_WITH_PIC;
            }

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setField(MultipleFields.ITEM_TYPE, type)
                    .setField(MultipleFields.ID, id)
                    .setField(MultipleFields.TITLE, title)
                    .setField(MultipleFields.TEXT, summary)
                    .setField(MultipleFields.DATE, pubDate)
                    .setField(MultipleFields.TAG, author)
                    .setField(MultipleFields.IMAGE_URL, image)
                    .build();

            ENTITIES.add(entity);
        }
        return ENTITIES;
    }
}
